package com.plian.system.service.sys.region.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.plian.Tools.CollectionUtil;
import com.plian.Tools.Func;
import com.plian.Tools.StringPool;
import com.plian.system.cache.RegionCache;
import com.plian.system.entity.sys.Region;
import com.plian.system.mapper.sys.RegionMapper;
import com.plian.system.service.sys.region.IRegionService;
import com.plian.system.vo.sys.RegionVO;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @Author: Take-off
 * @Description: TODO
 * @Date: 2023/2/8 3:48 PM
 * @Version: 1.0
 **/
@Service
@AllArgsConstructor
public class RegionServiceImpl extends ServiceImpl<RegionMapper, Region> implements IRegionService {

    @Override
    public boolean submit(Region region) {
        Integer cnt = baseMapper.selectCount(Wrappers.<Region>query().lambda().eq(Region::getCode, region.getCode()));
        if (cnt > 0) {
            return this.updateById(region);
        }
        // 设置祖区划编号
        Region parent = RegionCache.getByCode(region.getParentCode());
        if (Func.isNotEmpty(parent) || Func.isNotEmpty(parent.getCode())) {
            String ancestors = parent.getAncestors() + StringPool.COMMA + parent.getCode();
            region.setAncestors(ancestors);
        }
        // 设置省、市、区、镇、村
        Integer level = region.getRegionLevel();
        String code = region.getCode();
        String name = region.getName();
        if (level == RegionCache.PROVINCE_LEVEL) {
            region.setProvinceCode(code);
            region.setProvinceName(name);
        } else if (level == RegionCache.CITY_LEVEL) {
            region.setCityCode(code);
            region.setCityName(name);
        } else if (level == RegionCache.DISTRICT_LEVEL) {
            region.setDistrictCode(code);
            region.setDistrictName(name);
        } else if (level == RegionCache.TOWN_LEVEL) {
            region.setTownCode(code);
            region.setTownName(name);
        } else if (level == RegionCache.VILLAGE_LEVEL) {
            region.setVillageCode(code);
            region.setVillageName(name);
        }
        return this.save(region);
    }


    @Override
    public boolean removeRegion(String id) {
        Integer cnt = baseMapper.selectCount(Wrappers.<Region>query().lambda().eq(Region::getParentCode, id));
        if (cnt > 0) {
            throw new RuntimeException("请先删除子节点!");
        }
        return removeById(id);
    }

    @Override
    public List<RegionVO> lazyList(String parentCode, Map<String, Object> param) {
        List<RegionVO> regionVOS = baseMapper.lazyList(parentCode, param);
        if (CollectionUtil.isNotEmpty(regionVOS)){
            regionVOS.forEach(regionVO -> {
                regionVO.setId(Long.valueOf(regionVO.getCode()));
                regionVO.setParentId(Long.valueOf(regionVO.getParentCode()));
            });
        }
        return regionVOS;
    }

    @Override
    public List<RegionVO> lazyTree(String parentCode, Map<String, Object> param) {
        return baseMapper.lazyTree(parentCode, param);
    }




}
